<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Lib\Common;
use Lib\Wechat;

class UserController extends Controller
{
    //
    public function abc(){
        $token=(new Wechat())->checkAuth();
        return $token;
    }
    public function wechatValid(){
        (new Wechat())->valid();
    }

    /**
     * 微信公众号消息入口,消息入库必须是POST方式,而且必须跟微信验证(GET)同一个方法名称
     */
    public function wechatPostMessage(){
        $wechat=(new Wechat());
        $openid=$wechat->getRev()->getRevFrom();
        $content=$wechat->getRev()->getRevContent();
        if($content){
            $wechats=new Wechat();
            $kfList=$wechats->getCustomServiceOnlineKFlist();
//            Common::log(json_encode($kfList));
            //这里判断假如有客服在线的话,会发消息给客服
            if(!empty($kfList['kf_online_list'])){
                $json=['touser'=>$openid,'msgtype'=>'text','text'=>['content'=>$content]];
                $wechats=new Wechat();
                $data=$wechats->sendCustomMessage($json);
            }else{
                $wechat->text('对不起亲,没客服在线,请您稍微等等!')->reply();
            }


        }

//        $kefu=(new Wechat())->getCustomServiceOnlineKFlist();
//        Common::log(json_encode($kefu));
        /**
         * 获取用户回话信息
         */
//        $wechat=(new Wechat())->getRev();
//        $data=$wechat->getRevData();
//        $result=$wechat->getRevContent();
//        Common::log(json_encode($data));
//        Common::log(json_encode($result));

        /**
         * 客服部分
         */
        //转发多客服
//        $data=(new Wechat())->getRev()->transfer_customer_service();
//        Common::log(json_encode($data));


    }
    public function getKf(){
        $kefu=file_get_contents('https://www.sportkf.cn/abc');
        $wechat=new Wechat($kefu);
        $kefus=$wechat->getCustomServiceOnlineKFlist();
        dd($kefus);
    }
    //模板接口
    public function getTemplateList(){
        $kefu=file_get_contents('https://www.sportkf.cn/abc');
        $list=file_get_contents('https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token='.$kefu);
        dd(json_decode($list,true));
    }
    //用户列表
    public function getUserList(){
        $token=$this->getToken();
        $wechat=new Wechat($token);
        $user=$wechat->getUserInfo('oraUJw12IoH_Lw8p65L9cC25ljrc');
        dd($user);
    }

    /**
     * 通过服务器获取token
     * @return bool|string
     */
    private function getToken(){
        return file_get_contents('https://www.sportkf.cn/abc');
    }
    public function sendTemplate(){
        $openid='oraUJwx-E0bPp0binzKrKOAnDftM';
        $template_id='QcfBE1F48hR2pMT6NmSQhk9VWCuOYWamM0Ru5hEng-g';
        $data=['touser'=>$openid,'template_id'=>$template_id,'url'=>'http://www.baidu.com','data'=>['productType'=>['value'=>'小曾','color'=>'red'],'name'=>['value'=>'你好','color'=>'red'],'number'=>['value'=>'100圆','color'=>'red'],'expDate'=>['value'=>date('Y-m-d',time()),'color'=>'red'],'remark'=>['value'=>'123','color'=>'red']]];
        $result=(new Wechat($this->getToken()))->sendTemplateMessage($data);
        print_r( $result);
    }
}
